package com.eattravel.service.impl;

import com.eattravel.dto.PageInfoResult;
import com.eattravel.mapper.CommodityMapper;
import com.eattravel.mapper.OrderCommodityMapper;
import com.eattravel.mapper.OrderMapper;
import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.Order;
import com.eattravel.pojo.OrderCommodity;
import com.eattravel.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CommodityMapper commodityMapper;

    @Override
    public PageInfoResult<Order> findOrdersByUserId(String userId, PageInfoResult pageParameter ) {
        //设置分页参数
        PageHelper.startPage(pageParameter.getCurrPage(),pageParameter.getPageSize());
        //查询所有订单信息
        List<Order> orders=orderMapper.findOrdersByUserId(userId);
        //包装查询结果
        PageInfo<Order> pageInfo=new PageInfo<Order>(orders);
        //查询当前页数据信息
        List<Order> orderList=pageInfo.getList();
        //查询全部的页数
        pageParameter.setTotalSize(pageInfo.getTotal());
        //将获取当前夜页的数据封装进去
        pageParameter.setDataList(orderList);
        //将总页数封装进去
        pageParameter.setSizes(pageInfo.getPages());
        if(null!=pageParameter){
            return pageParameter;

        }
        return null;
    }

    @Override
    public List<Order> findOrdersByUserId(String userId) {

        List<Order> orders=orderMapper.findOrdersByUserId(userId);
        if(null!=orders){
            return orders;

        }
        return null;
    }

    @Override
    public boolean deleteOrderById(String orderId) {
        int count= orderMapper.deleteOrderById(orderId);
        if(count>0){

            return true;

        }
        return false;
    }



    // 支付宝部分
    @Override
    public int insertOrder(Order order) {
        int count= orderMapper.insertOrder(order);
        if(count>0){
            return count;
        }
        return 0;
    }
    @Override
    public int updateOrder(String orderId) {
       List<Commodity> list= commodityMapper.findCommodityByOrderId(orderId);
       for(int i=0;i<list.size();i++){
           commodityMapper.updataCommodityById(list.get(i).getCommodityId());
       }



        int count= orderMapper.updateOrder(orderId);
        if(count>0){
            return count;
        }
        return 0;
    }

    @Override
    public int insertIntoOrder(Order order,String userId) {
        int rs= orderMapper.insertIntoOrder(order,userId);
        return rs;
    }

    @Autowired
    OrderCommodityMapper orderCommodityMapper;
    @Override
    public int insertCommodity(String orderId,String commodityId,int commodityNum) {
        int rs = orderCommodityMapper.insertCommodity(orderId,commodityId,commodityNum);
        return rs;
    }
}
